//
//  RootMessageViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/8.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "RootMessageViewController.h"
#import "RootMessageTableViewCell.h"

@interface RootMessageViewController ()<UITableViewDelegate , UITableViewDataSource>

Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/

@end

//网络
@implementation RootMessageViewController (NetWorking)

- (void)removalMessage:(NSString *)um_id category:(NSString *)category{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"um_id":um_id,};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/User_Message/removalMessage.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
            ShowMessage(responseObject[@"msg"]);
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            
            [self getData:category];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

- (void)getData:(NSString *)type{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId,
                                 @"type":@"1",
                                 @"category":type,
                                 @"read_state":@"2"};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LaundryMall/User_Message/viewSystemMessages.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.dataArray removeAllObjects];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation RootMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"系统消息",@"交易消息",nil];
    
    //初始化UISegmentedControl
    [self getData:@"1"];
    self.navigationItem.titleView = ({
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
        segmentedControl.frame = CGRectMake(0, 0 , 200 , 30);
        // 设置默认选择项索引
        segmentedControl.selectedSegmentIndex = 0;
        [segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.layer.cornerRadius = 15;
        segmentedControl;
    });
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}


#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RootMessageTableViewCell *cell = [RootMessageTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    cell.getDeleteClick = ^{
      NSLog(@"删除");
        [self removalMessage:self.dataArray[indexPath.row][@"um_id"] category:self.dataArray[indexPath.row][@"category"]];
    };
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57 / WIDTH_5S_SCALE;
}

#pragma mark - click

-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
    
    NSInteger Index = Seg.selectedSegmentIndex;
 
    [self getData:[NSString stringWithFormat:@"%ld",(long)Index + 1]];

}


#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
